/*
   v4lcameras.h

   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.
 */

#ifndef V4LCAMERAS_H_
#define V4LCAMERAS_H_

#include <string>
#include <core/queues.h>
#include <core/monitors.h>
#include <wells/cameras.h>

//
// The basic strategy is to provide at least a stub of this class
// so we can instantiate it and figure things out at runtime.
//

#include <asm/types.h>          // for videodev2.h
#include <linux/videodev2.h>

namespace crossEyed {

namespace wells {

namespace TV4L {

using namespace std ;
using namespace crossEyed::core ;

class TYUYV
{
public:
   unsigned char Y0;
   unsigned char U0;
   unsigned char Y1;
   unsigned char V0;
};

class TFrame {
public:
   TYUYV *      data ;
   size_t       size ;
   unsigned int index ;
   bool         v4LEnqueued ;

   TFrame() : data(NULL), size(0), index(0), v4LEnqueued(false) { } ;

   size_t getSize() const { return size; } ;
};

class TSpecificCamera : public TCamera<TFrame>
{
protected:
   string devName ;
   int    fd ;

   int xioctl( int request, void * arg) ;

   size_t          numCameraFrames ;
   unsigned int    minNumFramesV4LEnqueued ;
   unsigned int    numV4LEnqueued ;

public:

   const static unsigned int DefaultMinNumFramesV4LEnqueued = 2 ;

protected:

   //
   // Vector for frames
   //
   std::vector<TFrame> frameVector ;

   TCountMonitor processingMonitor ;

public:

   TSpecificCamera( const char * pDevName,
                    size_t       numCameraFrames,
                    unsigned int minNumFramesV4LEnqueued = DefaultMinNumFramesV4LEnqueued ) ;

   ~TSpecificCamera();

   void configure();

   void qFrame(unsigned index);
   TFrame * dqFrame();

   const TCountMonitor & getProcessingMonitor() ;

   //
   // set capturing resolution
   //
   void setResolution( int dimX, int dimY ) ;
   void getResolution( int& dimX, int& dimY ) ;

   virtual void prepare() ;
   virtual void startStreaming() ;

   virtual void eventLoop() ;

   virtual void stopStreaming() ;
   virtual void release() ;
};

class TToLuminancePixelAdapter
{
protected:
   TFrame * pFrame ;
   TYUYV  * pCurMacroPixel ;

public:
   inline TToLuminancePixelAdapter( TFrame * pFrame )
      : pFrame( pFrame ),
        pCurMacroPixel( pFrame->data ) {};

   inline void toTarget( unsigned char aDestPixel[] )
   {
      aDestPixel[0] = pCurMacroPixel->Y0 ;
      aDestPixel[1] = pCurMacroPixel->Y1 ;
   }

   inline TToLuminancePixelAdapter & operator ++ (int)
   {
      pCurMacroPixel++ ;
      return *this ;
   }

   static inline size_t getBytesPerMacroPixel()
   {
      return sizeof( TV4L::TYUYV ) ;
   }

   static inline size_t getPixelsPerMacroPixel()
   {
      return 2 ;
   }
};

}}}
#endif /* V4LCAMERAS_H_ */
